package org.shopen.ai.puremvc.model.algo.containers
{
	import org.shopen.ai.puremvc.model.algo.ISearchNode;

	public class HashTable implements IHashTable
	{
		protected var data : Array;
		protected var _length : uint = 0;
		
		public function HashTable(numElements : uint = 100)
		{
			this.data = new Array(numElements);
			this._length = numElements;
		}

		public function itemAt(id:uint):ISearchNode
		{
			var result : ISearchNode = null;
			if (containsId(id))
			{
				result = data[id] as ISearchNode;
			}
			return result;
		}
		
		public function add(node:ISearchNode):uint
		{
			this.data[node.hash] = node;
			var result : uint = count();
			return result;
		}
		
		public function contains(node:ISearchNode):Boolean
		{
			var result : Boolean = containsId(node.hash);
			return result;
		}
		
		public function containsId(id:uint):Boolean
		{
			var result : Boolean = data[id] != null;
			return result;
		}
		
		public function count():uint
		{
			var result : int = 0;
			for each (var value : Object in this.data)
			{
				if (value != null)
				{
					result++;
				}
			}
			return result;
		}
		
		public function get length() : uint
		{
			return _length;
		}
		
		public function clear() : void
		{
			this.data = new Array(_length);
		}
		
	}
}